//
//  ZSY_HomeListViewController.m
//  zsy_BreadTrip
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZSY_HomeListViewController.h"
#import "ZSY_NearbyViewController.h"
#import "ZSY_HomeListTableViewCell.h"
#import "ZSY_HomeModel.h"
#import "ZSY_TravelProduct.h"
#import "ZSY_TwelveType.h"
#import "ZSY_FourType.h"
#import "ZSY_TravelFourTableViewCell.h"
#import "ZSY_TravelSevenTableViewCell.h"
#import "ZSY_TravelTwelveTableViewCell.h"
#import "ZSY_TypeModel.h"
#import "ZSY_HomeDetailViewController.h"
#import "ZSY_FourViewController.h"
#import "ZSY_AllStoryListViewController.h"
// 菊花类
#import "MBProgressHUD.h"
#import "ZSY_SearchViewController.h"
#import "ZSY_SearchUserModel.h"

#import "ZSY_HomeListScrollView.h"
#import "ZSY_HomeScrollViewDetailViewController.h"


@interface ZSY_HomeListViewController ()<UITableViewDataSource, UITableViewDelegate, ZSY_BaseTableViewCellDelegate
, UITextFieldDelegate, ZSY_HomeListScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray *scrollArray;
@property (nonatomic, retain) ZSY_BaseTableView *homeTableView;
@property (nonatomic, copy) NSString *storyTitle;
@property (nonatomic, copy) NSString *travelsTitle;
@property (nonatomic, retain) NSMutableArray *collectionArray;
@property (nonatomic, retain) ZSY_BaseLabel *sectionLabel;
@property (nonatomic, retain) NSMutableArray *allArray;

@property (nonatomic, assign) NSInteger nextPage;//记录下一页
@property(nonatomic,assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉
@property (nonatomic, retain) MBProgressHUD *HUB;


@end

@implementation ZSY_HomeListViewController

- (void)dealloc{
    [_allArray release];
    [_sectionLabel release];
    [_collectionArray release];
    [_storyTitle release];
    [_scrollArray release];
    [_homeTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    
    self.scrollArray = [NSMutableArray array];
    self.storyTitle = [NSString string];
    self.collectionArray = [NSMutableArray array];
    self.travelsTitle = [NSString string];
    self.allArray = [NSMutableArray array];
    
    // 右上角附近按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"附近" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    // 左上角所有按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem  alloc] initWithImage:[UIImage imageNamed:@"add_new_poi_search_button"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
  /*
    // 使用UITextField实现搜索功能
    self.searchField = [[ZSY_BaseTextField alloc] initWithFrame:CGRectMake(15, 5, WIDTH - 70, 34)];
    self.searchField.textColor = [UIColor colorWithRed:0 / 255.0 green:103 / 255.0 blue:155 / 255.0 alpha:1];
    self.searchField.font = [UIFont systemFontOfSize:16];
    self.searchField.layer.cornerRadius = 17;
    self.searchField.layer.masksToBounds = YES;
    self.searchField.backgroundColor = [UIColor lightGrayColor];
    self.searchField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    // textField文本发生变化时响应事件
    [self.searchField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventTouchDragInside];
    [self.searchField setReturnKeyType:UIReturnKeySearch];
    self.searchField.delegate = self;
    
    // 将textfield 添加到navigation上
    [self.navigationController.navigationBar addSubview:_searchField];
    [_searchField release];
   */
    CGRect frame = self.view.frame;
    frame.size.height -= 84;
    self.homeTableView = [[ZSY_BaseTableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    self.homeTableView.delegate = self;
    self.homeTableView.dataSource = self;
    self.homeTableView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:243 / 255.0 blue:233 / 255.0 alpha:0];
    [self.view addSubview:_homeTableView];
    // 注册cell
    [self.homeTableView registerClass:[ZSY_HomeListTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.homeTableView registerClass:[ZSY_TravelFourTableViewCell class] forCellReuseIdentifier:@"four"];
    [self.homeTableView registerClass:[ZSY_TravelSevenTableViewCell class] forCellReuseIdentifier:@"seven"];
    [self.homeTableView registerClass:[ZSY_TravelTwelveTableViewCell class] forCellReuseIdentifier:@"twelve"];
    // 取消cell 的分割线
    self.homeTableView.separatorStyle = NO;
    self.isUpLoading = NO;
    
    // 2.集成上拉下拉功能
    [self addHeader];
    [self addFooter];
    
#if 0 
    {
    // AFN请求数据
    [ZSY_AppTools creatingDataTask:@"http://api.breadtrip.com/v2/index/?lat=38.88262035734694&lng=121.5394745257025&sign=e5c96fa9bed9fca0d3b19b5a51476b0f" parameters:nil Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        NSMutableArray *elementsArray = [dataDic objectForKey:@"elements"];
        for (NSDictionary *elementsDic in elementsArray) {
            if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 1) {
                /**
                 *  首页上方两轮播图的数据
                 */
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSMutableArray *array in dataArray) {
                    for (NSDictionary *dic in array) {
                        NSString *image_url = [dic objectForKey:@"image_url"];
                        [self.scrollArray addObject:image_url];
                    }
                }
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 11) {
                /**
                 *  第一个section标题的数据
                 */
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    self.storyTitle = [dic objectForKey:@"title"];
                }
                
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 10) {
                /**
                 *  每日精选故事中的数据
                 */
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_HomeModel *homeModel = [[ZSY_HomeModel alloc] init];
                    [homeModel setValuesForKeysWithDictionary:dic];
                    [self.collectionArray addObject:homeModel];
                    [homeModel release];
                }
                
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 9) {
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    self.travelsTitle = [dic objectForKey:@"title"];
                }
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 7) {
                /**
                 type值为7的cell的数据
                 
                 - returns:
                 */
                ZSY_TravelProduct *product = [[ZSY_TravelProduct alloc] init];
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    product.type = @"7";
                    [product setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:product];
                    [product release];
                }
                
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 12) {
                /**
                 type值为12的cell数据
                 
                 - returns:
                 */
                ZSY_TwelveType *twelveType = [[ZSY_TwelveType alloc] init];
                NSArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    twelveType.type = @"12";
                    [twelveType setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:twelveType];
                    [twelveType release];
                }
            } else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 4) {
                /**
                 type值为4的cell的数据
                 
                 - returns:
                 */
                ZSY_FourType *fourType = [[ZSY_FourType alloc] init];
                NSArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    fourType.type = @"4";
                    [fourType setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:fourType];
                    [fourType release];
                }
            }
            
        }
        
        // 设置tableView的头部视图
        self.homeTableView.tableHeaderView = [self createScrollView];
        [self.homeTableView reloadData];
        
    }];
    }
#endif
    
//    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    [self.HUB show:YES];
//    self.HUB.labelText = @"正在加载..";
    
    
    [_homeTableView release];
}
#if 0
// 轮播图创建
- (ZSY_BaseView *)createScrollView {
    ZSY_BaseView *view = [[ZSY_BaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    scrollView.contentSize = CGSizeMake(WIDTH * 4, 200);
    for (int i = 0; i < _scrollArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, 200)];
        NSString *string = [self.scrollArray objectAtIndex:i];
        NSURL *url = [NSURL URLWithString:string];
            //        UIImage *image = [UIImage imageNamed:@"2233.jpg"];
            //        imageView.image = image;
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"2233"]];
        
        [scrollView addSubview:imageView];
        [imageView release];
    }
    scrollView.pagingEnabled = YES;
    [view addSubview:scrollView];
    [scrollView release];
//    [view release];
    return view;
}

#endif

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZSY_HomeListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.detailDelegate = self;
    if (indexPath.section == 0) {
        /**
         *  每日精选故事
         */
        if (_collectionArray.count != 0) {
            cell.collectionArray = _collectionArray;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        /**
         *  精彩游记和专题
         */
        ZSY_TypeModel *type = self.allArray[indexPath.row];
        NSLog(@"type = %@", type.type);
        if ([type.type isEqualToString:@"4"]) {
            ZSY_TravelFourTableViewCell *fourCell = [tableView dequeueReusableCellWithIdentifier:@"four"];
            ZSY_FourType *four = [_allArray objectAtIndex:indexPath.row];
            fourCell.four = four;
            fourCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return fourCell;
        } else if ([type.type isEqualToString:@"7"]) {
            ZSY_TravelSevenTableViewCell *sevenCell = [tableView dequeueReusableCellWithIdentifier:@"seven"];
            ZSY_TravelProduct *seven = [_allArray objectAtIndex:indexPath.row];
            sevenCell.seven = seven;
            sevenCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return sevenCell;
        } else {
            ZSY_TravelTwelveTableViewCell *twelveCell = [tableView dequeueReusableCellWithIdentifier:@"twelve"];
            ZSY_TwelveType *twelve = [_allArray objectAtIndex:indexPath.row];
            twelveCell.twelve = twelve;
            twelveCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return twelveCell;
        }
        
    }
}

#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    
    __block ZSY_HomeListViewController *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    // 添加下拉刷新头部控件
    [self.homeTableView addHeaderWithCallback:^{
        [self.allArray removeAllObjects];
        [self.scrollArray removeAllObjects];
        [self.collectionArray removeAllObjects];
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage = 1;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getDataList:vc.nextPage];//重新请求数据
        
    }];
    
    NSFileManager *manager1 = [NSFileManager defaultManager];
    
    NSString *filePath = [ZSY_AppTools createFilePathFromDocumentWithFolders:@[@"HomePage", @"HomeViewController", @"All"] fileName:@"home.txt"];
    //    NSLog(@"~~~~~%@", filePath);
    if ([manager1 fileExistsAtPath:filePath]) {
        NSData *data = [NSData dataWithContentsOfFile:filePath];
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        
        // 解析elements 首页显示的数据
        NSMutableArray *elementsArray = [dataDic objectForKey:@"elements"];
        for (NSDictionary *elementsDic in elementsArray) {
            if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 1) {
                /**
                 *  首页上方两轮播图的数据
                 */
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSMutableArray *array in dataArray) {
                    for (NSDictionary *dic in array) {
                        ZSY_HeaderModel *header = [[ZSY_HeaderModel alloc] init];
                        [header setValuesForKeysWithDictionary:dic];
                        [self.scrollArray addObject:header];
                        [header release];
                    }
                }
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 11) {
                /**
                 *  第一个section标题的数据
                 */
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    self.storyTitle = [dic objectForKey:@"title"];
                }
                
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 10) {
                /**
                 *  每日精选故事中的数据
                 */
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_HomeModel *homeModel = [[ZSY_HomeModel alloc] init];
                    [homeModel setValuesForKeysWithDictionary:dic];
                    [self.collectionArray addObject:homeModel];
                    [homeModel release];
                }
                
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 9) {
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    self.travelsTitle = [dic objectForKey:@"title"];
                }
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 7) {
                /**
                 type值为7的cell的数据
                 
                 - returns:
                 */
                ZSY_TravelProduct *product = [[ZSY_TravelProduct alloc] init];
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    product.type = @"7";
                    [product setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:product];
                    [product release];
                }
                
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 12) {
                /**
                 type值为12的cell数据
                 
                 - returns:
                 */
                ZSY_TwelveType *twelveType = [[ZSY_TwelveType alloc] init];
                NSArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    twelveType.type = @"12";
                    [twelveType setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:twelveType];
                    [twelveType release];
                }
            } else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 4) {
                /**
                 type值为4的cell的数据
                 
                 - returns:
                 */
                ZSY_FourType *fourType = [[ZSY_FourType alloc] init];
                NSArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    fourType.type = @"4";
                    [fourType setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:fourType];
                    [fourType release];
                }
            }
            
        }
        [self.homeTableView headerEndRefreshing];
        
        ZSY_HomeListScrollView *scrollView = [[ZSY_HomeListScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
        // 设置tableView的头部视图
        scrollView.myDelegate = self;
        scrollView.allArray = self.scrollArray;
        self.homeTableView.tableHeaderView = scrollView;
        [self.homeTableView reloadData];
        
    }
    
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    else{
        
        [self.homeTableView headerBeginRefreshing];
    }

    
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block ZSY_HomeListViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.homeTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage ++;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc getDataAddFooter:vc.nextPage];//请求数据
        
    }];
}



// cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZSY_HomeDetailViewController *homeDetailVC = [[ZSY_HomeDetailViewController alloc] init];
    ZSY_FourViewController *four = [[ZSY_FourViewController alloc] init];
    if (indexPath.section == 1) {
        
        ZSY_TypeModel *url = [_allArray objectAtIndex:indexPath.row];
        ZSY_TravelProduct *sevenType = [_allArray objectAtIndex:indexPath.row];
        homeDetailVC.share_url = url.share_url;
        if ([url.type isEqualToString:@"7"]) {
            four.url = sevenType.url;
            [self.navigationController pushViewController:four animated:YES];
        } else {
            [self.navigationController pushViewController:homeDetailVC animated:YES];
            
        }
    }
    [four release];
    [homeDetailVC release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return _allArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        return 410;
    }
    return 210;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
// 设置section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
// 设置section的内容, 样式
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ZSY_BaseView *sectionView = [[[ZSY_BaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)] autorelease];
    sectionView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:243 / 255.0 blue:233 / 255.0 alpha:0];
    ZSY_BaseView *leftView = [[ZSY_BaseView alloc] initWithFrame:CGRectMake(10, 15, 8, 20)];
    leftView.backgroundColor = [UIColor cyanColor];
    leftView.layer.cornerRadius = 3;
    leftView.layer.masksToBounds = YES;
    [sectionView addSubview:leftView];
    [leftView release];
    
    self.sectionLabel = [[ZSY_BaseLabel alloc] initWithFrame:CGRectMake(30, 15, 100, 20)];
    if (section == 0) {
        
        self.sectionLabel.text = _storyTitle;
    } else if (section == 1) {
        self.sectionLabel.text = _travelsTitle;
    }
    [self.sectionLabel sizeToFit];
    self.sectionLabel.font = [UIFont systemFontOfSize:15];
    [sectionView addSubview:_sectionLabel];
    [_sectionLabel release];
    if (section == 0) {
    
    ZSY_BaseLabel *rightLabel = [[ZSY_BaseLabel alloc] initWithFrame:CGRectMake(WIDTH - 50, 15, 25, 20)];
    rightLabel.text = @"全部";
    rightLabel.textColor = [UIColor lightGrayColor];
    rightLabel.font = [UIFont systemFontOfSize:12];
    [sectionView addSubview:rightLabel];
    [rightLabel release];
    
    ZSY_BaseImageView *rightImageView = [[ZSY_BaseImageView alloc] initWithFrame:CGRectMake(WIDTH - 25, 15, 15, 20)];
//    rightImageView.backgroundColor = [UIColor blackColor];
    rightImageView.image = [UIImage imageNamed:@"section_icon"];
    [sectionView addSubview:rightImageView];
    [rightImageView release];
    
    ZSY_BaseButton *rightButton = [ZSY_BaseButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(WIDTH - 50, 15, 40, 20);
    rightButton.backgroundColor = [UIColor clearColor];
        [rightButton addTarget:self action:@selector(rightAllButton:) forControlEvents:UIControlEventTouchUpInside];
    [sectionView addSubview:rightButton];
    }
    return sectionView;
}

- (void)rightAllButton:(ZSY_BaseButton *)button {
    NSLog(@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=0");
    NSLog(@"正在点击全部按钮");
    ZSY_AllStoryListViewController *allStoryVC = [[ZSY_AllStoryListViewController alloc] init];
    [self.navigationController pushViewController:allStoryVC animated:YES];
    [allStoryVC release];
    
}
// 设置section头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return _storyTitle;
    }else {
        return _travelsTitle;
    }
}

// 右上角附近按钮实现
- (void)rightButtonAction:(UIBarButtonItem *)button {
    self.hidesBottomBarWhenPushed = YES;
    
    ZSY_NearbyViewController *nearbyVC = [[ZSY_NearbyViewController alloc] init];
    [self.navigationController pushViewController:nearbyVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    [nearbyVC release];
}

// 左上角搜索按钮实现
- (void)leftButtonAction:(UIBarButtonItem *)button {
    ZSY_SearchViewController *searchVC = [[ZSY_SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    [searchVC release];
}

// 实现协议方法
- (void)pushToDetailView:(NSString *)share_url {
    ZSY_HomeDetailViewController *homeDetailVC = [[ZSY_HomeDetailViewController alloc] init];
    homeDetailVC.share_url = share_url;
    
    [self.navigationController pushViewController:homeDetailVC animated:YES];
    [homeDetailVC release];
}

- (void)method:(NSString *)headerModel {
    ZSY_HomeScrollViewDetailViewController *vc = [[ZSY_HomeScrollViewDetailViewController alloc] init];
    vc.html_url = headerModel;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

#pragma 用AFN请求数据
- (void)getDataList:(NSInteger)nextCursor {
    // AFN请求数据
    [ZSY_AppTools creatingDataTask:@"http://api.breadtrip.com/v2/index/?lat=38.88262035734694&lng=121.5394745257025&sign=e5c96fa9bed9fca0d3b19b5a51476b0f" parameters:nil Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        NSDictionary *dataDic = [dic objectForKey:@"data"];
  
        // 解析elements 首页显示的数据
        NSMutableArray *elementsArray = [dataDic objectForKey:@"elements"];
        for (NSDictionary *elementsDic in elementsArray) {
            if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 1) {
                /**
                 *  首页上方两轮播图的数据
                 */
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSMutableArray *array in dataArray) {
                    for (NSDictionary *dic in array) {
                        ZSY_HeaderModel *header = [[ZSY_HeaderModel alloc] init];
                        [header setValuesForKeysWithDictionary:dic];
                        [self.scrollArray addObject:header];
                        [header release];
                    }
                }
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 11) {
                /**
                 *  第一个section标题的数据
                 */
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    self.storyTitle = [dic objectForKey:@"title"];
                }
                
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 10) {
                /**
                 *  每日精选故事中的数据
                 */
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_HomeModel *homeModel = [[ZSY_HomeModel alloc] init];
                    [homeModel setValuesForKeysWithDictionary:dic];
                    [self.collectionArray addObject:homeModel];
                    [homeModel release];
                }
                
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 9) {
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    self.travelsTitle = [dic objectForKey:@"title"];
                }
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 7) {
                /**
                 type值为7的cell的数据
                 
                 - returns:
                 */
                ZSY_TravelProduct *product = [[ZSY_TravelProduct alloc] init];
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    product.type = @"7";
                    [product setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:product];
                    [product release];
                }
                
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 12) {
                /**
                 type值为12的cell数据
                 
                 - returns:
                 */
                ZSY_TwelveType *twelveType = [[ZSY_TwelveType alloc] init];
                NSArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    twelveType.type = @"12";
                    [twelveType setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:twelveType];
                    [twelveType release];
                }
            } else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 4) {
                /**
                 type值为4的cell的数据
                 
                 - returns:
                 */
                ZSY_FourType *fourType = [[ZSY_FourType alloc] init];
                NSArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    fourType.type = @"4";
                    [fourType setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:fourType];
                    [fourType release];
                }
            }
            
        }
        [self.homeTableView footerEndRefreshing];
        [self.homeTableView headerEndRefreshing];

        ZSY_HomeListScrollView *scrollView = [[ZSY_HomeListScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
        // 设置tableView的头部视图
        scrollView.myDelegate = self;
        scrollView.allArray = self.scrollArray;
        self.homeTableView.tableHeaderView = scrollView;
        [self.homeTableView reloadData];
        
        NSData *data = [NSJSONSerialization dataWithJSONObject:resuposeObject options:NSJSONWritingPrettyPrinted error:nil];
        NSString *filePath = [ZSY_AppTools createFilePathFromDocumentWithFolders:@[@"HomePage", @"HomeViewController", @"All"] fileName:@"home.txt"];
        [ZSY_AppTools saveDataOnLocal:data localPath:filePath];
        
    }];

}

#pragma mark - 上拉数据解析
- (void)getDataAddFooter:(NSInteger)next {
    // AFN请求数据
    [ZSY_AppTools creatingDataTask:[NSString stringWithFormat:@"http://api.breadtrip.com/v2/index/?lat=38.88256814493554&lng=121.5395097188763&next_start=%ld&sign=0b16301b85d780d4c804663867c0aa88",next] parameters:nil Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        NSDictionary *dic = resuposeObject;
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        
        // 解析elements 首页显示的数据
        NSMutableArray *elementsArray = [dataDic objectForKey:@"elements"];
        for (NSDictionary *elementsDic in elementsArray) {
            if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 9) {
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    self.travelsTitle = [dic objectForKey:@"title"];
                }
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 7) {
                /**
                 type值为7的cell的数据
                 
                 - returns:
                 */
                ZSY_TravelProduct *product = [[ZSY_TravelProduct alloc] init];
                NSMutableArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    product.type = @"7";
                    [product setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:product];
                    [product release];
                }
                
            }else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 12) {
                /**
                 type值为12的cell数据
                 
                 - returns:
                 */
                ZSY_TwelveType *twelveType = [[ZSY_TwelveType alloc] init];
                NSArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    twelveType.type = @"12";
                    [twelveType setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:twelveType];
                    [twelveType release];
                }
            } else if (((NSNumber *)[elementsDic objectForKey:@"type"]).intValue == 4) {
                /**
                 type值为4的cell的数据
                 
                 - returns:
                 */
                ZSY_FourType *fourType = [[ZSY_FourType alloc] init];
                NSArray *dataArray = [elementsDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    fourType.type = @"4";
                    [fourType setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:fourType];
                    [fourType release];
                }
            }
            
        }
        [self.homeTableView footerEndRefreshing];
        
        
        [self.homeTableView reloadData];
        
//        NSData *data = [NSJSONSerialization dataWithJSONObject:resuposeObject options:NSJSONWritingPrettyPrinted error:nil];
//        NSString *filePath = [ZSY_AppTools createFilePathFromDocumentWithFolders:@[@"HomePage", @"HomeViewController", @"All"] fileName:@"homeAdd.txt"];
//        [ZSY_AppTools saveDataOnLocal:data localPath:filePath];
        
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
